package com.hnweaver.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnweaver.Util.FlowUtil;
import com.hnweaver.Util.LogInfoUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * FileName: InvoiceVerificAtion
 *
 * @author lhf
 * @version 1.00 版本
 * @date 2023/9/12 10:00
 */
public class InvoiceVerificAtion implements Action {
    private static final LogInfoUtil log = new LogInfoUtil("InvoiceVerificAtion");
    private final FlowUtil flowUtil = new FlowUtil();
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestId = requestInfo.getRequestid();
        log.logWriter("Action requestId =" + requestId);
        String mainTableName = requestInfo.getRequestManager().getBillTableName();
        JSONObject mainTableJson = flowUtil.getFormProperty(requestInfo);
        String subTableName = mainTableName + "_dt1";
        JSONObject detailsTables = flowUtil.getDetailsTables(requestInfo, subTableName);
        JSONArray jsonArray = detailsTables.getJSONArray(subTableName);
        List<String> list = new ArrayList<String>();
        RecordSet rs = new RecordSet();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String fph = jsonObject.getString("fph");//发票号
            String qsrq = jsonObject.getString("qsrq");//起始日期
            String bxbb = jsonObject.getString("bxbb");//报销币别
            String[] fphs=fph.split(",");
            for (int j = 0; j < fphs.length; j++) {
                rs.execute("select billingdate,invoicenumber,invoicetype from fnainvoiceledger where id = '" + fphs[j] + "'");
                if (rs.next()) {
                    String billingdate = Util.null2String(rs.getString("billingdate"));
                    String invoicetype = Util.null2String(rs.getString("invoicetype"));
                    if(invoicetype.equals("5")){//定额发票跳过判断
                        continue;
                    }
                    log.logWriter("billingdate =" + billingdate);
                    if (billingdate.length()<=0) {
                        requestInfo.getRequestManager().setMessagecontent(Util.null2String(rs.getString("invoicenumber"))+"开票日期不允许为空");
                        return FAILURE_AND_CONTINUE;
                    }
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date billingdate1 = null;
                    Date qsrq1 = null;
                    try {
                        billingdate1 = df.parse(billingdate.substring(0,7)+"-00 00:00:00");
                        qsrq1 = df.parse(qsrq.substring(0,7)+"-00 00:00:00");
                    } catch (ParseException e) {
                        requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                        return FAILURE_AND_CONTINUE;
                    }
                    if("PRE001".equals(bxbb)){
                        if(billingdate1.compareTo(qsrq1)<0){
                            list.add(Util.null2String(rs.getString("invoicenumber")));
                        }
                    }
                }
            }
            log.logWriter("------list---->" + list.toString());
            if(list.size()>0){
                requestInfo.getRequestManager().setMessagecontent("以下发票日期不允许早于起始日期月份：\n"+list.toString());
                return FAILURE_AND_CONTINUE;
            }
        }
        return SUCCESS;
    }
}
